package com.ychen.ruijee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ychen.ruijee.bean.Category;
import com.ychen.ruijee.bean.Setmeal;
import com.ychen.ruijee.bean.SetmealDish;
import com.ychen.ruijee.common.R;
import com.ychen.ruijee.dao.SetmealDao;
import com.ychen.ruijee.dao.SetmealDishDao;
import com.ychen.ruijee.dto.SetmealDto;
import com.ychen.ruijee.service.CategoryService;
import com.ychen.ruijee.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ychen@gotop.cn
 * @date 2024-06-04 0:06
 */
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {

    @Resource
    private SetmealDishDao setmealDishDao;

    @Resource
    private CategoryService categoryService;

    /**
     * 套餐信息分页查询展示
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> setmealPageInfo(Integer page, Integer pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> pageDtoInfo = new Page<>();

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name !=null,Setmeal::getName,name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        this.page(pageInfo,wrapper);

        log.info("【Setmeal】分页对象中 的 records的值为：{}",pageInfo.getRecords());
        // 对象拷贝
        BeanUtils.copyProperties(pageInfo,pageDtoInfo,"records");
        List<Setmeal> records = pageInfo.getRecords();
        // 将 records + some 封装到 pageDtoInfo 中去
        List<SetmealDto> list = records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category != null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        Page<SetmealDto> setmealDtoPage = pageDtoInfo.setRecords(list);
        return setmealDtoPage;
    }

    /**
     * 保存套餐信息
     *
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional
    public boolean saveSetmeal(SetmealDto setmealDto) {
        log.info("【=========> 要保存的 套餐信息是 {}】",setmealDto.toString());
        // 先判断表中是否 存在 要新增的套餐
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getName,setmealDto.getName());
        int count = this.count(wrapper);
        try {
            if(count > 0){
                return false;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // 新增到套餐表
        boolean saveFlag = this.save(setmealDto);
        // 保存 setmeal_id 到 setmeal_dish 表中
        Long setmealDtoId = setmealDto.getId();
        setmealDto.getSetmealDishes().stream().forEach((item)->{
            // 每个菜品 关联当前的 套餐 id
            item.setSetmealId(String.valueOf(setmealDtoId));
            setmealDishDao.insert(item);
        });

        return saveFlag;
    }

    /**
     * 修改套餐信息时 数据回显
     *
     * @param id
     * @return
     */
    @Override
    public R<SetmealDto> getSetmealDto(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        // 从 setmeal_dish 表中 查询 dish 信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishDao.selectList(wrapper);
        setmealDto.setSetmealDishes(setmealDishes);
        return R.success(setmealDto);
    }

    /**
     * 套餐批量删除
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public boolean deleteSetmeal(List<Long> ids) {
        // 查询套餐状态  确定是否可用删除
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        // select * from setmeal where id in (1,2,3) and status = 1
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        int count = this.count(wrapper);
        try {
            if (count > 0){
                return false;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        boolean removeSetmeal = this.removeByIds(ids);
        // 删除 关联表 setmeal_dish
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId,ids);
        // 删除 setmeal_dish 中 setmeal 对应的数据信息
        int deleteSetmealDish = setmealDishDao.deleteBatchIds(ids);

        return deleteSetmealDish >0 && removeSetmeal;
    }

}
